clear all;
clc;

load mnist_test;
clear xU;

s = 50;
c = 10;
Y = yU';
clear yU;

nump = c*(c-1)/2;
sz = zeros(c,1);
idx = cell(10,1);
for i = 1:c
    idx{i}=find(Y==(i-1));
    sz(i)=size(idx{i},1);
end

fval = zeros(c,c);
vial = zeros(c,c);

load 7nneigsplus;
clear Dvec;
clear IDXvec;

V = V(60001:70000,:);

for i = 1:c
    for j = i+1:c
        ix = [idx{i};idx{j}];
        Yt = [ones(sz(i),1);-ones(sz(j),1)];
        l = sz(i)+sz(j);
        P = repmat(Yt,1,s).*V(ix,1:s);
        cvx_begin
        cvx_solver sedumi
            variable x(s,1)
            variable b
            variable y(l,1) nonnegative
            minimize (sum(y));
            P*x+b*Yt+y >= ones(l,1);
        cvx_end
        fval(i,j) = cvx_optval;
        vial(i,j) = sum(y>0);
    end
end

save(['res/feat_test_pairS',int2str(s)],'fval','vial');